package com.cd.hospital.www.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.cd.hospital.common.annotation.SysLog;
import com.cd.hospital.common.exception.RRException;
import com.cd.hospital.modules.entity.SysOssEntity;
import com.cd.hospital.www.oss.cloud.OSSFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cd.hospital.modules.entity.AdvertisementEntity;
import com.cd.hospital.modules.service.AdvertisementService;
import com.cd.hospital.common.utils.PageUtils;
import com.cd.hospital.common.utils.Query;
import com.cd.hospital.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 * 
 * @author weirdor
 * @email 179946422@qq.com
 * @date 2017-09-18 23:57:46
 */
@RestController
@RequestMapping("/modules/advertisement")
public class AdvertisementController {
	@Autowired
	private AdvertisementService advertisementService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("modules:advertisement:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<AdvertisementEntity> advertisementList = advertisementService.queryList(query);
		int total = advertisementService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(advertisementList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}


	/**
	 * 上传文件
	 */
	@SysLog("上传文件")
	@RequestMapping("/upload")
	public R upload(@RequestParam("file") MultipartFile file) throws Exception {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}

		//上传文件
		String url = OSSFactory.build().upload(file.getBytes());

		//保存文件信息
		AdvertisementEntity ossEntity = new AdvertisementEntity();
		ossEntity.setUrl(url);
		ossEntity.setCreateDate(new Date());
		ossEntity.setUppateDate(new Date());
		ossEntity.setFlag(1);
		advertisementService.save(ossEntity);
		return R.ok().put("url", url);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("modules:advertisement:info")
	public R info(@PathVariable("id") Long id){
		AdvertisementEntity advertisement = advertisementService.queryObject(id);
		
		return R.ok().put("advertisement", advertisement);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("modules:advertisement:save")
	public R save(@RequestBody AdvertisementEntity advertisement){
		advertisementService.save(advertisement);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("modules:advertisement:update")
	public R update(@RequestBody AdvertisementEntity advertisement){
		advertisementService.update(advertisement);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("modules:advertisement:delete")
	public R delete(@RequestBody Long[] ids){
		advertisementService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
